//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (strong, nonatomic) NSMutableArray *dataArr;

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //进入前台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeNotes:) name:UIApplicationWillEnterForegroundNotification object:nil];
    //已经激活的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeNotes:) name:UIApplicationDidBecomeActiveNotification object:nil];
    //进入后台的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeNotes:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    //将取消活动的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeNotes:) name:UIApplicationWillResignActiveNotification object:nil];
    [self showOnTextView];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self setUpBackground];
}

-(void)setUpBackground{
    NSUserDefaults *m = [NSUserDefaults standardUserDefaults];
    float red = [[m valueForKey:@"redslider"] floatValue]/255.0;
    float green = [[m valueForKey:@"greenslider"] floatValue]/255.0;
    float blue = [[m valueForKey:@"blueslider"] floatValue]/255.0;
    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
    NSLog(@"%f,%f,%f",red*255.0,green*255.0,blue*255.0);
}

//懒加载数据
-(NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]]) {
            _dataArr = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else{
            _dataArr = [NSMutableArray array];
        }
    }
    return _dataArr;
}

//文件路径
-(NSString *)plistPath{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"note.plist"];
    return filePath;
}
//保存数据
-(void)save{
    [self.dataArr writeToFile:[self plistPath] atomically:YES];
}

//显示时间方法
-(NSString *)dateTime{
    NSDate *date = [NSDate date];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"HH:mm:ss"];
    NSString *dateStr = [f stringFromDate:date];
    return dateStr;
}

//收到通知的处理方法
-(void)takeNotes:(NSNotification *)noti{
    //进入前台的通知
    if ([noti.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
        NSString *dataStr = [self dateTime];
        NSString *str = [dataStr stringByAppendingString:@"  进入前台"];
        [self.dataArr insertObject:str atIndex:0];
        [self save];
        [self showOnTextView];
        
    }
    //已经激活的通知
    else if ([noti.name isEqualToString:UIApplicationDidBecomeActiveNotification]){
        NSString *dataStr = [self dateTime];
        NSString *str = [dataStr stringByAppendingString:@"  已经激活"];
        [self.dataArr insertObject:str atIndex:0];
        [self save];
        [self showOnTextView];
    }
    //
    else if ([noti.name isEqualToString:UIApplicationDidEnterBackgroundNotification]){
        NSString *dataStr = [self dateTime];
        NSString *str = [dataStr stringByAppendingString:@"  已经进入后台"];
        [self.dataArr insertObject:str atIndex:0];
        [self save];
        [self showOnTextView];
    }
    else {
        NSString *dataStr = [self dateTime];
        NSString *str = [dataStr stringByAppendingString:@"  将取消活动"];
        [self.dataArr insertObject:str atIndex:0];
        [self save];
        [self showOnTextView];
    }
}

//在textView上显示的方法
-(void)showOnTextView{
    NSMutableString *mutStr = [NSMutableString string];
    for (int i = 0; i < self.dataArr.count; i++) {
        [mutStr appendString:self.dataArr[i]];
        [mutStr appendString:@"\n"];
    }
    self.textView.text = mutStr;
    NSLog(@"%@",self.textView.text);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
